#!/bin/sh

set -ue

LDFLAGS="${LDFLAGS:-}"
[ -n "$VERBOSE" ] && set -x

if [ "$#" = 0 ]; then
	echo "Assuming the working directory contains source files." >&2
fi

if [ -n "$TAR" ]; then
	tar -x >&2
	(
	[ -n "${1:-}" ] && [ -d "src/$1" ] && cd "src/$1"
	/usr/local/go/bin/go get -v -d >&2
	)
fi

/usr/local/go/bin/go build -a -ldflags "-linkmode external -extldflags \"-static\" -s -w $LDFLAGS" "$@" >&2

if [ -n "$TAR" ]; then
	tar -c .
fi
